<template>
  <el-button @click="handleAdd" v-if="$filters.auth($filters.PrivilegeType.Add)" class="button-header"  type="primary" :icon="Plus">添加分类</el-button>
  <el-form inline :model="query" class="filter" label-width="100px">
    <el-form-item label="分类名称：">
      <el-col :span="22">
        <el-input v-model="query.name" placeholder="请输入分类名称" />
      </el-col>
    </el-form-item>
    <el-form-item class="filter-actions">
      <el-button class="button-small" type="primary" @click="handleSearch">筛选</el-button>
      <el-button type="text" class="button-reset" @click="handleClear">重置筛选条件</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData"  @sort-change="handleSortChange">
      <el-table-column prop="name" label="分类名称" />
      <el-table-column label="排序" sortable prop="sequence">
      <template #default="scope">
        <el-input-number v-model="scope.row.sequence" controls-position="right" @change="handleChangeSequence(scope.row)" :min="0" :max="99999999" />
      </template>
    </el-table-column>
    <el-table-column label="操作" align="right" width="200"  v-if="$filters.auth($filters.PrivilegeType.Edit) || $filters.auth($filters.PrivilegeType.Delete)">
      <template #default="scope">
        <el-button type="text" v-if="$filters.auth($filters.PrivilegeType.Edit)" @click="handleEdit(scope.row)">编辑</el-button>
        <el-popconfirm :width="300" v-if="$filters.auth($filters.PrivilegeType.Delete)" class="btn-popconfirm" @confirm="handleDelete(scope.row.id)" confirmButtonText="确认" cancelButtonText="取消" title="确定要删除该员工吗？">
          <template #reference>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
   </el-table>
   <el-dialog append-to-body v-model="postForm.dialogVisible" :title="postForm.title" width="30%">
    <el-form ref="postFormRef" label-width="150px" class="ruleForm" :model="postForm.form" :rules="postForm.rules">
      <el-form-item label="分类名称：" prop="name">
          <el-col :span="22">
            <el-input v-model="postForm.form.name" maxlength="20" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
        </el-form-item>
        <el-form-item label="序号：">
          <el-col :span="22">
            <el-input-number v-model="postForm.form.sequence" :min="0" controls-position="right"/>
          </el-col>
        </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="postForm.dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave(postFormRef)">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import {Plus} from '@element-plus/icons-vue'
import {reactive,ref,onMounted} from 'vue'
import { ExampleCategorySave,ExampleCategoryDelete,ExampleCategoryUpdateSequence,GetExampleCategorys } from '@/api/example'
import {ElForm, ElMessage} from 'element-plus'
import { deepClone } from '@/utils'
/** 查询条件 */
const query = reactive({
  name:''
})
const tableData = ref<CategoryInfo[]>([])
/**
 * 添加编辑表单
 */

const postForm = ref<DialogForm<CategoryInfo>>({
dialogVisible: false,
title:'添加分类',
form: {
  id: 0,
  name: '',
  sequence: 0
},
rules: {
  name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
}
})
type FormInstance = InstanceType<typeof ElForm>
const postFormRef = ref<FormInstance>()
const handleAdd =():void => {
  postForm.value.form =  {
    id: 0,
    name: '',
    sequence: 0
  }
  postForm.value.dialogVisible = true
  postForm.value.title = '添加分类'
}
const handleEdit = (row: CategoryInfo):void => {
  postForm.value.form =  {
    id: row.id,
    name: row.name,
    sequence: row.sequence,
  }
  postForm.value.dialogVisible = true
  postForm.value.title = '编辑分类'
}
let isAsc = false
const handleSave = (formEl: FormInstance | undefined): void => {
  if (!formEl) return
  formEl.validate((valid) => {
    if(valid) {
      ExampleCategorySave(postForm.value.form).then((res:ServiceResponse<any>) => {
        if(res.success) {
          ElMessage.success('保存成功')
          loadData()
          postForm.value.dialogVisible = false
        } else  ElMessage.error(res.msg)
      })
    }
  })
}
const loadData = ():void => {
  GetExampleCategorys(query.name).then((res:ServiceResponse<CategoryInfo[]>) => {
    tableData.value = res.data ?? []
    if(isAsc) 
        tableData.value =tableData.value.sort(function(a, b) { return a.sequence - b.sequence })
      else
        tableData.value =tableData.value.sort(function(a, b) { return b.sequence - a.sequence })
  })
}
const handleChangeSequence = (row:CategoryInfo):void => {
 let category = deepClone(row)
 ExampleCategoryUpdateSequence(category.id,category.sequence).then((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('保存成功')
          loadData()
        } else  ElMessage.error(res.msg)
  })
}
const handleSortChange = (data: any) => {
  isAsc = data.order === 'ascending'
  loadData()
}
const handleDelete = (id:number):void => {
  ExampleCategoryDelete(id).then(((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('删除成功')
          loadData()
        } else  ElMessage.error(res.msg)
  }))
}
const handleSearch = ():void => {
  loadData()
}
const handleClear = ():void => {
  query.name = ''
  loadData()
}
onMounted(() => {
  loadData()
})
</script>
<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
@import "@/assets/styles/tablePage.scss";
</style>